﻿using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using Silent.Utilities;

namespace Silent.Screenshot
{
    public class ManualScreenSaver : Form
    {
        protected IContainer components = null;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        protected Rectangle cropRect;

        public ManualScreenSaver()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            const string name = "ScreenForm";
            SuspendLayout();
            AutoScaleDimensions = new SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(284, 262);
            Name = name;
            Text = name;
            ResumeLayout(false);
            Paint += ScreenFormsPaint;
            KeyDown += ScreenFormKeyDown;
            MouseDown += ScreenFormMouseDown;
            MouseMove += ScreenFormMouseMove;
            MouseUp += ScreenFormMouseUp;
            FormBorderStyle = FormBorderStyle.None;
            ShowInTaskbar = false;
            TopMost = true;
            DoubleBuffered = true;
            Height = Screen.PrimaryScreen.Bounds.Height;
            Width = Screen.PrimaryScreen.Bounds.Width;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void ScreenFormsPaint(object sender, PaintEventArgs e)
        {
            using (var brush = new SolidBrush(Color.FromArgb(75, 100, 100, 100)))
            {
                e.Graphics.FillRectangle(brush, cropRect);
            }
        }
        private void ScreenFormKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }
        private void ScreenFormMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                cropRect = new Rectangle(e.X, e.Y, 0, 0);
        }
        private void ScreenFormMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                cropRect = new Rectangle(cropRect.Left, cropRect.Top, e.X - cropRect.Left, e.Y - cropRect.Top);
            Invalidate();
        }
        private void ScreenFormMouseUp(object sender, MouseEventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private Bitmap CropImage(Bitmap source, Rectangle section)
        {
            var bmp = new Bitmap(section.Width, section.Height);
            Graphics.FromImage(bmp).DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);
            return bmp;
        }
        public Bitmap CropScreenShot()
        {
            var bmpScreenshot = Screenshooter.MakeScreenShot();
            BackgroundImage = bmpScreenshot;
            Cursor = Cursors.Cross;
            return ShowDialog() == DialogResult.Cancel ? null : CropImage(bmpScreenshot, cropRect);
        }
        public void SaveScreenShot(string filepath)
        {
            var bmpScreenshot = CropScreenShot();
            if (bmpScreenshot != null)
                bmpScreenshot.Save(filepath, ImageFormat.Png);
        }
    }
}
